Ext.namespace('VAS');
/**
 * 数据发布窗口
 * @class VAS.CheckTreePanel
 * @extends VAS.TreePanel
 */
 VAS.CheckTreePanel=Ext.extend(VAS.TreePanel,{
 	
   initComponent:function(){
		var _this=this;
		var config={
			autoRoll:true
			,height :  _this.height
			,listeners : {
				'checkchange' : function(node, checked) {
				if (checked) {
					node.getUI().addClass('complete');
				} else {
					node.getUI().removeClass('complete');
				};
				node.expand();
				node.attributes.checked = checked;
				node.eachChild(function(child) {
					child.ui.toggleCheck(checked);
					child.attributes.checked = checked;
					child.fireEvent('checkchange', child, checked);
					});
				}
			}
		};	
		
		Ext.apply(this, config);
		Ext.apply(this.initialConfig, config);
		VAS.CheckTreePanel.superclass.initComponent.apply(this, arguments); 
	},
	
	//递归获取第一级节点
	getlevel1ParentsNode : function(node,obj){
		var _this = this;
		var parentNode = node.parentNode
		if(parentNode.id=='root'){
			if(!node.attributes.checked){
				if (obj.sleNodeId == "") {
					sleNodeId += node.id;
				} else {
					obj.sleNodeId += ","+node.id;
				}
			}
		}
		else {
			_this.getlevel1ParentsNode(parentNode,obj);
		}
	},
	
	getCheckedNode:function(){
		var _this = this;
		var obj = {};
		obj.sleNodeId = "";
		var selNodes = _this.getChecked();
		Ext.each(selNodes, function(node){
			if (node.id != "root") {
				if(!node.leaf){
					node.expand();
				  node.eachChild(function(child) {
						child.fireEvent('checkchange', child, child.attributes.checked);
					});
				}
			}
		});
		
		var _selNodes = this.getChecked();
		Ext.each(_selNodes, function(node){
			if (node.id != "root") {			
				if (obj.sleNodeId == "") {
					obj.sleNodeId += node.id;
				} else {
					obj.sleNodeId += ","+node.id;
				}
			}
			_this.getlevel1ParentsNode(node,obj);
		});
		return obj.sleNodeId;
	}

});

VAS.PublishWin = function($config) {
	var _this = this;
	var $height;

	_this.getPostdata=function(){		
     	var rightIds = menuPanel.getCheckedNode();
    	postdata.rightIds = rightIds;
    	return postdata;
	};
	_this.callback=function(){
	   _this.close();
	   $config.grid.reload();
	};
	
	_this._getHeight = function(_height,_y) {
		if (typeof(_height) !== "undefined") {
			if (typeof(_height)==='string'&&_height.indexOf('%') > -1) {
				$height = document.body.clientHeight -_y-247;			
			} else if(typeof(_height)==='number'){
				$height = _height - 50;
			}
		} 
		return $height;
	}
	
	_this._getWinHeight = function(_height,_y) {
		if (typeof(_height) !== "undefined") {
			if (typeof(_height)==='string'&&_height.indexOf('%') > -1) {
				$height = document.body.clientHeight -_y-155;			
			} else if(typeof(_height)==='number'){
				$height = _height - 50;
			}
		} 
		return $height;
	}
	
	_this._getWinWidth = function(_width,_x) {			
		var $width;
		if (typeof(_width) !== "undefined") {
			if (typeof(_width)==='string'&&_width.indexOf('%') > -1) {
			$width = document.body.clientWidth -_x-130;	
			} else if(typeof(_width)==='number'){
				$width = _width;
			}
		} 
		return $width;
	}

	var menuPanel = new VAS.CheckTreePanel({
		height : _this._getHeight($config.height,2),
		url : $config.menuPanel.url
	});
	var checkbox = new Ext.form.Checkbox({
		style: {
            marginLeft : '8px'
        },
		boxLabel : '同时发布栏目下文章'
		,height : 20
		,checked:true
	});
	
    var isPublishing=0;
    var intervalId;
	var publishButton = new Ext.Button({
		id : 'publish'
		,text : '发布'
		,handler : function() {
			if('' == menuPanel.getCheckedNode() || 'undefined' == menuPanel.getCheckedNode()){
				Ext.MessageBox.alert('提示','请选择要发布的对象');
				return false;
			};
			Ext.Ajax.request({
				url : $config.menuPanel.action,
			    success: function(response) {
			       //检查session是否过期
				   checkTimeOut(response);
				   var responseObj = Ext.util.JSON.decode(response.responseText);
				   if (responseObj.success) {
				    _this.body.mask(responseObj.msg,"x-mask-loading");
						intervalId=window.setInterval(getReleaseStatus,5000);
						isPublishing=1;
				   } else {
				   		alertFailure(responseObj);
				   }
			   }, failure: function(response) {
					Ext.Msg.alert('提示','服务器异常！');
			   },
			   params: {
					data : Ext.util.JSON.encode(_this.getPostdata()),
					doArticle : checkbox.checked
			   }
			});
		}
	});
	
	function getReleaseStatus (){
		Ext.Ajax.request({
			url : 'admin/release/release!getReleaseStatus.action',
		    success: function(response) {
		    	//检查session是否过期
				checkTimeOut(response);
			    var responseObj = Ext.util.JSON.decode(response.responseText);
			   	if(responseObj.isIdle){
			   		_this.body.unmask();
			   		Ext.Msg.alert('提示', responseObj.msg);
			   		if(isPublishing == 1){
			   			isPublishing = 0;
			   			window.clearInterval(intervalId);
			   		}
			   } 
		   }, failure: function(response) {
				Ext.Msg.alert('提示','服务器异常！');
		   },
		    
		   params: {}
		});
	}
	
	var thisConfig = {
		frame : true,
		animate : true,
		enableDD : true,
		autoScroll : true,
		autoRoll:true,
		constrain : true
		,height : _this._getWinHeight($config.height,$config.y)
		,items : [menuPanel,checkbox]	
		,buttons : [publishButton],
		buttonAlign : "center"
	}
	var _config = $config || {};
	Ext.apply(_this, Ext.applyIf(thisConfig, _config));
	VAS.PublishWin.superclass.constructor.call(_this);
};

Ext.extend(VAS.PublishWin, VAS.BasicWindow, {
	manager:wgroup
});
Ext.reg('publish-win', VAS.PublishWin);
